"""
解析Yaml格式的测试数据
"""
import os.path
import yaml
from collections import OrderedDict
from tests.utils.logUtil import log


# 解析 YAML 测试数据
def parsing_yaml_test_data(endpoint: str, file_path: str):
    """
    加载指定端点的测试数据
    :param endpoint: 端点名称
    :param file_path:
    :return:
    """
    if not os.path.exists(file_path):
        log.error(f"{endpoint} file {file_path} is not found ")
        return []

    try:
        with open(file_path, "r", encoding='utf-8') as file:
            data = yaml.safe_load(file)
            if data is None:
                log.error(f"YAML data in {file_path} is empty or malformed.")
                return []

            # 获取指定端点的测试数据并转换为符合 pytest 的格式
            endpoint_data = data.get(endpoint, [])
            formatted_data = []

            for item in endpoint_data:
                # 动态根据键值对处理，确保顺序一致
                ordered_item = OrderedDict(item)
                formatted_data.append(tuple(ordered_item.get(key, None) for key in ordered_item.keys()))

            return formatted_data

    except Exception as e:
        log.error(f"Error reading YAML file {file_path}: {e}")
        return []


def load_yaml(file_path: str):
    """
    使用 path 读取 YAML 文件并返回其内容作为字符串。

    :param file_path: YAML 文件的路径
    :return: 文件内容的字符串表示
    """
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
        return content
    except FileNotFoundError:
        print(f"File not found: {file_path}")
        return None
    except IOError as e:
        print(f"Error reading file: {e}")
        return None


if __name__ == '__main__':
    # 调用示例
    path = "../data/user_test_data.yaml"  # 确保文件路径正确
    test_data = parsing_yaml_test_data("info", path)
    print(test_data)

    config_path = '../configs/cloud-admin-management.yaml'
    yaml_content = load_yaml(config_path)
    print(yaml_content)
